<HTML>
<HEAD>
<TITLE>Listing of usart1.c</TITLE>
<LINK rel=stylesheet href="udf_styles.css" type="text/css">
</HEAD>
<BODY>
<table class="">
<tr><td><code><i>     1</i> </code></td><td><code>&nbsp </code></td></tr>
<tr><td><code><i>     2</i> </code></td><td><code>&nbsp </code></td></tr>
<tr><td><code><i>     3</i> </code></td><td><code>&nbsp #if&nbsp defined(__XC)</code></td></tr>
<tr><td><code><i>     4</i> </code></td><td><code>&nbsp &nbsp&nbsp&nbsp&nbsp #include&nbsp&lsaquo  xc.h&rsaquo &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp /*&nbsp XC8&nbsp General&nbsp Include&nbsp File&nbsp */</code></td></tr>
<tr><td><code><i>     5</i> </code></td><td><code>&nbsp #elif&nbsp defined(HI_TECH_C)</code></td></tr>
<tr><td><code><i>     6</i> </code></td><td><code>&nbsp &nbsp&nbsp&nbsp&nbsp #include&nbsp&lsaquo  htc.h&rsaquo &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp /*&nbsp HiTech&nbsp General&nbsp Include&nbsp File&nbsp */</code></td></tr>
<tr><td><code><i>     7</i> </code></td><td><code>&nbsp #endif</code></td></tr>
<tr><td><code><i>     8</i> </code></td><td><code>&nbsp </code></td></tr>
<tr><td><code><i>     9</i> </code></td><td><code>&nbsp #include&nbsp&lsaquo  stdint.h&rsaquo &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp /*&nbsp For&nbsp uint8_t&nbsp definition&nbsp */</code></td></tr>
<tr><td><code><i>    10</i> </code></td><td><code>&nbsp #include&nbsp&lsaquo  stdbool.h&rsaquo  </code></td></tr>
<tr><td><code><i>    11</i> </code></td><td><code>&nbsp #include&nbsp "usart1.h"</code></td></tr>
<tr><td><code><i>    12</i> </code></td><td><code>&nbsp #include&nbsp "system.h"</code></td></tr>
<tr><td><code><i>    13</i> </code></td><td><code>&nbsp </code></td></tr>
<tr><td><code><i>    14</i> </code></td><td><code>&nbsp void&nbsp configura_USART&nbsp (void){</code></td></tr>
<tr><td><code><i>    15</i> </code></td><td><code>&nbsp &nbsp //TODO&nbsp debo&nbsp configurar&nbsp RB2&nbsp para&nbsp usar&nbsp como&nbsp IO,&nbsp si&nbsp se&nbsp puede&nbsp al&nbsp usar&nbsp USART&nbsp (!!Leer&nbsp datasheet!!)</code></td></tr>
<tr><td><code><i>    16</i> </code></td><td><code>&nbsp </code></td></tr>
<tr><td><code><i>    17</i> </code></td><td><code>&nbsp &nbsp&nbsp&nbsp&nbsp //Calculo&nbsp la&nbsp carga&nbsp del&nbsp registro&nbsp en&nbsp base&nbsp al&nbsp baudrate&nbsp elegido&nbsp baudrate</code></td></tr>
<tr><td><code><i>    18</i> </code></td><td><code>&nbsp //FOSC=_XTAL_FREQ</code></td></tr>
<tr><td><code><i>    19</i> </code></td><td><code>&nbsp //X=SPBRG</code></td></tr>
<tr><td><code><i>    20</i> </code></td><td><code>&nbsp //BAUDRATE=FOSC/(64(X+1))&nbsp //en&nbsp low&nbsp speed</code></td></tr>
<tr><td><code><i>    21</i> </code></td><td><code>&nbsp //BAUDRATE=FOSC/(16(X+1))&nbsp //en&nbsp high&nbsp speed</code></td></tr>
<tr><td><code><i>    22</i> </code></td><td><code>&nbsp //X=FOSC/64/BAUDRATE-1&nbsp en&nbsp low&nbsp speed</code></td></tr>
<tr><td><code><i>    23</i> </code></td><td><code>&nbsp //X=FOSC/16/BAUDRATE-1&nbsp en&nbsp low&nbsp speed</code></td></tr>
<tr><td><code><i>    24</i> </code></td><td><code>&nbsp &nbsp //Si&nbsp FOSC=20MHZ</code></td></tr>
<tr><td><code><i>    25</i> </code></td><td><code>&nbsp //X=20000000/64/2400-1</code></td></tr>
<tr><td><code><i>    26</i> </code></td><td><code>&nbsp </code></td></tr>
<tr><td><code><i>    27</i> </code></td><td><code>&nbsp //X=129&nbsp para&nbsp low&nbsp speed</code></td></tr>
<tr><td><code><i>    28</i> </code></td><td><code>&nbsp SPBRG=&nbsp _XTAL_FREQ/64/baudrate-1;</code></td></tr>
<tr><td><code><i>    29</i> </code></td><td><code>&nbsp //SPBRG=129;</code></td></tr>
<tr><td><code><i>    30</i> </code></td><td><code>&nbsp &nbsp&nbsp&nbsp&nbsp /*TXSTAbits.CSRC=1;&nbsp /*Asynchronous&nbsp mode</code></td></tr>
<tr><td><code><i>    31</i> </code></td><td><code>&nbsp Don?t&nbsp care</code></td></tr>
<tr><td><code><i>    32</i> </code></td><td><code>&nbsp Synchronous&nbsp mode</code></td></tr>
<tr><td><code><i>    33</i> </code></td><td><code>&nbsp 1&nbsp =&nbsp Master&nbsp mode&nbsp (Clock&nbsp generated&nbsp internally&nbsp from&nbsp BRG)</code></td></tr>
<tr><td><code><i>    34</i> </code></td><td><code>&nbsp 0&nbsp =&nbsp Slave&nbsp mode&nbsp (Clock&nbsp from&nbsp external&nbsp source)*/</code></td></tr>
<tr><td><code><i>    35</i> </code></td><td><code>&nbsp </code></td></tr>
<tr><td><code><i>    36</i> </code></td><td><code>&nbsp /*TXSTAbits.TX9=0;/*9-bit&nbsp Transmit&nbsp Enable&nbsp bit</code></td></tr>
<tr><td><code><i>    37</i> </code></td><td><code>&nbsp &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp 1&nbsp =&nbsp Selects&nbsp 9-bit&nbsp transmission</code></td></tr>
<tr><td><code><i>    38</i> </code></td><td><code>&nbsp &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp 0&nbsp =&nbsp Selects&nbsp 8-bit&nbsp transmission*/</code></td></tr>
<tr><td><code><i>    39</i> </code></td><td><code>&nbsp RCSTAbits.RX9=0;&nbsp /*9-bit&nbsp Transmit&nbsp Enable&nbsp bit*/</code></td></tr>
<tr><td><code><i>    40</i> </code></td><td><code>&nbsp TXSTAbits.SYNC=0;/*&nbsp USART&nbsp Mode&nbsp Select&nbsp bit</code></td></tr>
<tr><td><code><i>    41</i> </code></td><td><code>&nbsp 1&nbsp =&nbsp Synchronous&nbsp mode</code></td></tr>
<tr><td><code><i>    42</i> </code></td><td><code>&nbsp 0&nbsp =&nbsp Asynchronous&nbsp mode*/</code></td></tr>
<tr><td><code><i>    43</i> </code></td><td><code>&nbsp </code></td></tr>
<tr><td><code><i>    44</i> </code></td><td><code>&nbsp </code></td></tr>
<tr><td><code><i>    45</i> </code></td><td><code>&nbsp /*TXSTAbits.BRGH=0;&nbsp /*High&nbsp Baud&nbsp Rate&nbsp Select&nbsp bit</code></td></tr>
<tr><td><code><i>    46</i> </code></td><td><code>&nbsp Asynchronous&nbsp mode</code></td></tr>
<tr><td><code><i>    47</i> </code></td><td><code>&nbsp 1&nbsp =&nbsp High&nbsp speed</code></td></tr>
<tr><td><code><i>    48</i> </code></td><td><code>&nbsp 0&nbsp =&nbsp Low&nbsp speed</code></td></tr>
<tr><td><code><i>    49</i> </code></td><td><code>&nbsp Synchronous&nbsp mode</code></td></tr>
<tr><td><code><i>    50</i> </code></td><td><code>&nbsp Unused&nbsp in&nbsp this&nbsp mode*/</code></td></tr>
<tr><td><code><i>    51</i> </code></td><td><code>&nbsp //TXSTAbits.TX9D=1;//&nbsp 9th&nbsp bit&nbsp of&nbsp transmit&nbsp data.&nbsp Can&nbsp be&nbsp parity&nbsp bit.</code></td></tr>
<tr><td><code><i>    52</i> </code></td><td><code>&nbsp //Note:&nbsp SREN/CREN&nbsp overrides&nbsp TXEN&nbsp in&nbsp SYNC&nbsp mode.</code></td></tr>
<tr><td><code><i>    53</i> </code></td><td><code>&nbsp &nbsp </code></td></tr>
<tr><td><code><i>    54</i> </code></td><td><code>&nbsp </code></td></tr>
<tr><td><code><i>    55</i> </code></td><td><code>&nbsp /*TXSTAbits.TXEN=1;&nbsp /*Transmit&nbsp Enable&nbsp bit(1)</code></td></tr>
<tr><td><code><i>    56</i> </code></td><td><code>&nbsp 1&nbsp =&nbsp Transmit&nbsp enabled</code></td></tr>
<tr><td><code><i>    57</i> </code></td><td><code>&nbsp 0&nbsp =&nbsp Transmit&nbsp disabled*/</code></td></tr>
<tr><td><code><i>    58</i> </code></td><td><code>&nbsp RCSTAbits.CREN=1;</code></td></tr>
<tr><td><code><i>    59</i> </code></td><td><code>&nbsp RCSTAbits.SPEN=1;</code></td></tr>
<tr><td><code><i>    60</i> </code></td><td><code>&nbsp }</code></td></tr>
<tr><td><code><i>    61</i> </code></td><td><code>&nbsp </code></td></tr>
<tr><td><code><i>    62</i> </code></td><td><code>&nbsp </code></td></tr>
<tr><td><code><i>    63</i> </code></td><td><code>&nbsp //prototipo&nbsp de&nbsp la&nbsp funcion&nbsp PUTCH&nbsp para&nbsp poder&nbsp enviar&nbsp al&nbsp USART&nbsp con&nbsp printf</code></td></tr>
<tr><td><code><i>    64</i> </code></td><td><code>&nbsp void&nbsp putch(char&nbsp data)&nbsp {</code></td></tr>
<tr><td><code><i>    65</i> </code></td><td><code>&nbsp &nbsp while(&nbsp !&nbsp TXIF)</code></td></tr>
<tr><td><code><i>    66</i> </code></td><td><code>&nbsp &nbsp continue;</code></td></tr>
<tr><td><code><i>    67</i> </code></td><td><code>&nbsp &nbsp TXREG&nbsp =&nbsp data;</code></td></tr>
<tr><td><code><i>    68</i> </code></td><td><code>&nbsp }</code></td></tr>
<tr><td><code><i>    69</i> </code></td><td><code>&nbsp &nbsp char&nbsp getch(void)&nbsp {</code></td></tr>
<tr><td><code><i>    70</i> </code></td><td><code>&nbsp &nbsp&nbsp&nbsp&nbsp return&nbsp RCREG;</code></td></tr>
<tr><td><code><i>    71</i> </code></td><td><code>&nbsp }</code></td></tr>
<tr><td><code><i>    72</i> </code></td><td><code>&nbsp </code></td></tr>
<tr><td><code><i>    73</i> </code></td><td><code>&nbsp &nbsp __bit&nbsp kbhit&nbsp (void){&nbsp //el&nbsp ide&nbsp no&nbsp reconoce&nbsp __bit&nbsp pero&nbsp funciona</code></td></tr>
<tr><td><code><i>    74</i> </code></td><td><code>&nbsp &nbsp&nbsp&nbsp&nbsp&nbsp return&nbsp RCIF;</code></td></tr>
<tr><td><code><i>    75</i> </code></td><td><code>&nbsp &nbsp }</code></BODY>
</HTML>
